package com.tech.connect.zhaorencai;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.ksy.common.view.ProxyLayout;
import com.ksy.common.view.PullToRefreshLayout;
import com.tech.connect.R;
import com.tech.connect.activity.SearchActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.RenMaiHttp;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.model.ItemRenMai;
import com.tech.connect.model.PagerModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RenCaiRenMaiFragment extends BaseFragment {

    private BaseAdapter<ItemRenMai, BaseHolder> adapter;
    private RecyclerView recycler;
    private int page = 1;
    private int pageSize = 15;
    private ProxyLayout<RecyclerView> proxyLayout;
    private List<ItemRenMai> allList = new ArrayList<>();

    enum Type {
        TongXiang,
        TongHang,
        XiaoYou
    }

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_rencai_renmai, null);
        return view;
    }

    @Override
    protected boolean isShowHeadBar() {
        return true;
    }

    @Override
    protected boolean isFullStateBar() {
        return true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getHeadBar().setLeftIcon(R.drawable.icon_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        }).setTitle("人脉").setRightIcon(R.drawable.icon_search, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(SearchActivity.class);
            }
        });
        initView(view);

        loadData();
    }


    private void initView(View view) {
        final View llAll = view.findViewById(R.id.llAll);
        final View llNear = view.findViewById(R.id.llNear);
        final View tvAll = view.findViewById(R.id.tvAll);
        final View tvNear = view.findViewById(R.id.tvNear);
        final TabLayout tabLayout = view.findViewById(R.id.tabLayout);
        recycler = view.findViewById(R.id.recycler);
        proxyLayout = new ProxyLayout<>(activity, recycler);
        proxyLayout.setCanRefresh(true);
        proxyLayout.setDragListener(new PullToRefreshLayout.DragListener() {
            @Override
            public void drag2Upper(PullToRefreshLayout pullToRefreshLayout) {
                ++page;
                loadData();
            }

            @Override
            public void drag2Downer(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                loadData();
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        recycler.addItemDecoration(new HLineDivider());
        llAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llAll.setBackgroundResource(R.drawable.common_shape_corner5_stroke_yellow);
                llNear.setBackgroundResource(R.drawable.common_shape_corner5_stroke_white);
                tvAll.setSelected(true);
                tvNear.setSelected(false);
            }
        });
        llNear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llAll.setBackgroundResource(R.drawable.common_shape_corner5_stroke_white);
                llNear.setBackgroundResource(R.drawable.common_shape_corner5_stroke_yellow);
                tvAll.setSelected(false);
                tvNear.setSelected(true);
            }
        });
        llAll.performClick();
        final TabLayout.Tab tab2 = tabLayout.newTab();
        final TabLayout.Tab tab3 = tabLayout.newTab();
        final TabLayout.Tab tab4 = tabLayout.newTab();
        tab2.setText("同乡");
        tab3.setText("同行");
        tab4.setText("校友");

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                allList.clear();
                initAdapter();
                if (tab == tab2) {
                    loadTongXiang();
                } else if (tab == tab3) {
                    loadTonghang();
                } else if (tab == tab4) {
                    loadXiaoyou();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        tabLayout.addTab(tab2);
        tabLayout.addTab(tab3);
        tabLayout.addTab(tab4);
        tab2.select();
    }


    private void initAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<ItemRenMai, BaseHolder>(R.layout.item_layout_renmai, allList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemRenMai item = allList.get(position);
                    ImageView ivHead = helper.getView(R.id.ivHead);
                    TextView tvName = helper.getView(R.id.tvName);
                    TextView tvFriend = helper.getView(R.id.tvFriend);
                    TextView tvJob = helper.getView(R.id.tvJob);
                    TextView tvCount = helper.getView(R.id.tvCount);
                    if (position % 3 == 0) {
                        tvFriend.setText("+好友");
                        tvFriend.setBackgroundResource(R.drawable.common_shape_corner5_orange);
                    } else {
                        tvFriend.setText("好友");
                        tvFriend.setBackgroundResource(R.drawable.common_shape_corner5_gray);
                    }
                    ImageLoader.getInstance().loadHead(activity, item.headImage, ivHead);
                    tvName.setText(item.nickName);
//                    tvJob.setText(item.);
                }
            };
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }

    private void loadData() {
        loadTongXiang();
        loadXiaoyou();
        loadTonghang();
    }

    private void loadTongXiang() {
        Map<String, Object> map = RenMaiHttp.getMap();
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        RenMaiHttp.tongxiang(map, new BaseEntityOb<PagerModel<ItemRenMai>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemRenMai> data, String msg) {
                dealData(isSuccess, data);
            }
        });
    }

    private void loadXiaoyou() {
        Map<String, Object> map = RenMaiHttp.getMap();
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        RenMaiHttp.xiaoyou(map, new BaseEntityOb<PagerModel<ItemRenMai>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemRenMai> data, String msg) {
                dealData(isSuccess, data);
            }
        });
    }

    private void loadTonghang() {
        Map<String, Object> map = RenMaiHttp.getMap();
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        RenMaiHttp.tonghang(map, new BaseEntityOb<PagerModel<ItemRenMai>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemRenMai> data, String msg) {
                dealData(isSuccess, data);
            }
        });
    }

    private void dealData(boolean isSuccess, PagerModel<ItemRenMai> data) {
        List<ItemRenMai> list = null;
        if (isSuccess && data != null) {
            list = data.content;
        }
        if (page == 1) {
            allList.clear();
        }
//        initAdapter(list);
        if (list != null && list.size() >= pageSize) {
            proxyLayout.setCanLoadMore(true);
        } else {
            proxyLayout.setCanLoadMore(false);
        }
        if (allList.isEmpty()) {
            proxyLayout.showEmptyView();
        } else {
            proxyLayout.showContentView();
        }
        proxyLayout.dragFinish();
    }

}